<script setup>
import {
  Edit,
  Delete,
  Close
} from '@element-plus/icons-vue'


import {ref} from 'vue'


const contentTypes = ref([])


const versionSeasons = ref([])
const accountTypes = ref([
  {
    "id": 1,
    "resourceValue": "官方账号",
  },
  {
    "id": 0,
    "resourceValue": "用户账号",
  }
])

const addAccountTypes = ref([
  {
    "id": 1,
    "resourceValue": "官方账号",
  }
])


//用户搜索时选中的分类id
const accountType = ref('')

const account = ref('')

//用户搜索时选中的发布状态
const state = ref('')


const rewardSeasonId = ref('')

const mileStones = ref([])


//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(15)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size
  userList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  userList()
}


import {versionSeasonListService} from '@/api/versionSeason.js'

import {
  addOfficialAccountService,
  updateOfficialAccountService,
  userListService,
  userSuspendAccountService, userUnblockAccountService
} from '@/api/userAccount.js'



import {Plus} from '@element-plus/icons-vue'
import {QuillEditor} from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const dataModel = ref({})


//导入token
import {useTokenStore} from '@/stores/token.js';

const tokenStore = useTokenStore();


//上传成功的回调函数
const uploadHeadPicSuccess = (result) => {
  dataModel.value.headPic = result.data.fileUrl;
  dataModel.value.headPicName = result.data.fileName;
}

const uploadFramePicSuccess = (result) => {
  dataModel.value.headFramePic = result.data.fileUrl;
  dataModel.value.headFramePicName = result.data.fileName;
}



//添加文章
import {ElMessage} from 'element-plus'

const addOfficialAccount = async (clickState) => {
  //把发布状态赋值给数据模型
  dataModel.value.state = clickState;
  dataModel.value.personalSignaturePic =fileList;
  //调用接口
  let result = await addOfficialAccountService(dataModel.value);

  ElMessage.success(result.msg ? result.msg : '添加成功');

  //让抽屉消失
  visibleDrawer.value = false;

  //刷新当前列表
  userList()
}

//编辑分类
const updateOfficialAccount = async () => {

  dataModel.value.fileUrl =fileList;
  //调用接口
  let result = await updateOfficialAccountService(dataModel.value);

  ElMessage.success(result.msg ? result.msg : '修改成功')

  //调用获取所有分类的函数
  userList();

  //隐藏弹窗
  visibleDrawer.value = false;
}


//获取文章列表数据
const userList = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    account: account.value ? account.value : null,
    accountType: accountType.value !=='' ? accountType.value : null

  }
  let result = await userListService(params);

  total.value = result.data.total;
  mileStones.value = result.data.records;

}



userList();



const suspendAccount = (row) => {
  //提示用户  确认框

  ElMessageBox.confirm(
      '你确认要封禁该账号吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        //调用接口
        let params = {
          id: row.userId
        }
        let result = await userSuspendAccountService(params);
        ElMessage({
          type: 'success',
          message: '封禁成功',
        })
        //刷新列表
        userList()
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '用户取消了封禁',
        })
      })
}


const unblockAccount = async (row) => {
  let params = {
    id: row.userId
  }
  let result = await userUnblockAccountService(params);
  ElMessage.success('解封成功');
  userList()

}


//删除分类
import {ElMessageBox} from 'element-plus'
import {orderRefundService} from "@/api/orderMember.js";
import {interestCancelPopularService} from "@/api/interestPublishRecordData.js";


//定义变量,控制标题的展示
const title = ref('')
const showMileStone = (row) => {
  visibleDrawer.value = true, title.value = '编辑用户'
  dataModel.value.userId= row.userId;
  dataModel.value.official =row.official;
  dataModel.value.name =row.name;
  dataModel.value.personalSignature =row.personalSignature;
  fileList.value =[];
  for (let i = 0; i < row.personalSignaturePic.length; i++) {
    fileList.value.push({
      url:row.personalSignaturePic[i].url,
      name:row.personalSignaturePic[i].name
    })
  }
  dataModel.value.headPic =row.headPic;
  dataModel.value.headFramePic =row.headFramePic;

}

//清空模型的数据
const clearData = () => {

  dataModel.value.userId= '';
  dataModel.value.official ='';
  dataModel.value.name ='';
  dataModel.value.personalSignature ='';
  dataModel.value.headPic ='';
  dataModel.value.headFramePic='';
  fileList.value =[];
}


const delHeadPicImg = () => {
  dataModel.value.headPic = '';
  dataModel.value.headPicName = '';
}

const delHeadFramePicImg = () => {
  dataModel.value.headFramePic = '';
  dataModel.value.headFramePicName = '';
}


const fileList = ref([])

</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>账号管理</span>
        <div class="extra">
          <el-button type="primary" @click="visibleDrawer = true; title = '添加用户'; clearData()">添加用户
          </el-button>
        </div>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="账号类型：">
        <el-select placeholder="请选择" v-model="accountType">
          <el-option v-for="c in accountTypes" :key="c.id" :label="c.resourceValue" :value="c.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="账号：">
        <el-input  v-model="account" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="userList">搜索</el-button>
        <el-button @click="accountType = '';account=''">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 文章列表 -->
    <el-table :data="mileStones" border style="width: 100%">
      <el-table-column label="用户id" prop="userId" width="100"></el-table-column>
      <el-table-column label="账号" prop="account" width="100"></el-table-column>
      <el-table-column label="用户名称" prop="name" width="100"></el-table-column>
      <el-table-column label="密码" prop="password" width="100"></el-table-column>
      <el-table-column label="imei" prop="imei" width="200"></el-table-column>
      <el-table-column label="账号类型"  width="100">
        <template v-slot="scope">
          <span v-if="scope.row.official==1">官方账号</span>
          <span v-else >用户账号</span>
        </template>
      </el-table-column>
      <el-table-column label="会员类型" width="100">
        <template v-slot="scope">
          <span v-if="scope.row.memberType==0">普通用户</span>
          <span v-if="scope.row.memberType==1">月会员</span>
          <span v-if="scope.row.memberType==2">半年会员</span>
          <span v-if="scope.row.memberType==3">年会员</span>
          <span v-else ></span>
        </template>
      </el-table-column>
      <el-table-column label="会员过期时间" prop="expireTime"  width="200"></el-table-column>
      <el-table-column label="性别"  width="70">
        <template v-slot="scope">
          <span v-if="scope.row.sex==1">女</span>
          <span v-if="scope.row.sex==0">男</span>
          <span v-else ></span>
        </template>
      </el-table-column>
      <el-table-column label="个性签名文字" prop="personalSignature" width="200"></el-table-column>

      <el-table-column label="个性签名图片" width="200">
        <template v-slot="scope">
          <span v-for=" c in scope.row.personalSignaturePic" >
             <img v-if="c.url" :src="c.url" width="40" height="40" class="head_pic" />
          </span>
        </template>
      </el-table-column>

      <el-table-column label="头像">
        <template v-slot="scope">
          <img v-if="scope.row.headPic" :src="scope.row.headPic" width="40" height="40" class="head_pic" />
        </template>
      </el-table-column>

      <el-table-column label="头像框">
        <template v-slot="scope">
          <img v-if="scope.row.headFramePic" :src="scope.row.headFramePic" width="40" height="40" class="head_pic" />
        </template>
      </el-table-column>

      <el-table-column label="粉丝数" prop="fansNum" width="100"></el-table-column>


      <el-table-column label="创建时间" width="200px" prop="createTime"></el-table-column>

      <el-table-column label="操作" width="150px">
        <template #default="{ row }">
          <el-button :icon="Edit" v-if="row.official ==1" circle plain type="primary" @click="showMileStone(row)"></el-button>
        </template>
      </el-table-column>
      <el-table-column label="封号"  width="250px">
        <template #default="{ row }">
          <el-button  v-if="row.status ===0 && row.official ===0"   type="danger" @click="suspendAccount(row)">封号</el-button>
          <el-button  v-if="row.status ===1 && row.official ===0"   type="primary" @click="unblockAccount(row)">解封账号</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"/>
      </template>
    </el-table>


    <!-- 分页条 -->
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5, 10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total"
                   @size-change="onSizeChange"
                   @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end"/>


    <!-- 抽屉 -->
    <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
      <el-form :model="dataModel" label-width="100px">
        <el-form-item label="账号类型">
          <el-select placeholder="请选择" v-model="dataModel.official ">
            <el-option v-for="c in addAccountTypes" :key="c.id" :label="c.resourceValue" :value="c.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="dataModel.name" type="text" style="width: 210px;"/>
        </el-form-item>

        <el-form-item label="个性签名文字">
          <el-input v-model="dataModel.personalSignature" type="textarea" style="width: 500px"/>
        </el-form-item>

        <el-form-item label="个性签名图片" v-if="showState != 0">

          <el-upload
              v-model:file-list="fileList"
              class="upload-demo"
              action="/api/admin/upload/uploadFile"
              list-type="picture"
          >
            <el-button type="primary">上传图片</el-button>
            <template #tip>

            </template>
          </el-upload>
        </el-form-item>


        <el-form-item label="头像">
          <el-upload class="avatar-uploader" :auto-upload="true" :show-file-list="false"
                     action="/api/admin/upload/uploadFile/v2" name="file"
                     :headers="{ 'Authorization': tokenStore.token }"
                     :on-success="uploadHeadPicSuccess">
            <img v-if="dataModel.headPic" :src="dataModel.headPic" class="avatar"/>
            <el-icon v-else class="avatar-uploader-icon">
              <Plus/>
            </el-icon>
          </el-upload>
          <span v-if="show">{{ dataModel.headPic }}</span>
          <el-icon v-if="dataModel.headPic" @click="delHeadPicImg()">
            <Close/>
          </el-icon>
        </el-form-item>

        <el-form-item label="头像框">
          <el-upload class="avatar-uploader" :auto-upload="true" :show-file-list="false"
                     action="/api/admin/upload/uploadFile/v2" name="file"
                     :headers="{ 'Authorization': tokenStore.token }"
                     :on-success="uploadFramePicSuccess">
            <img v-if="dataModel.headFramePic" :src="dataModel.headFramePic" class="avatar"/>
            <el-icon v-else class="avatar-uploader-icon">
              <Plus/>
            </el-icon>
          </el-upload>
          <span v-if="show">{{ dataModel.headFramePic }}</span>
          <el-icon v-if="dataModel.headFramePic" @click="delHeadFramePicImg()">
            <Close/>
          </el-icon>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="title == '添加用户' ? addOfficialAccount() : updateOfficialAccount()">保存</el-button>

        </el-form-item>
      </el-form>
    </el-drawer>


  </el-card>
</template>
<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}


/* 抽屉样式 */
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }

    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}

.editor {
  width: 100%;

  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>